package com.linewell.export.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

/**
 * @Title: DruidConfiguration
 * @description:
 * @author: pzhongqaing@linewell.com
 * @since: 2020年09月21日 19:36
 */

@Configuration
public class DruidConfiguration {

    Logger logger = LoggerFactory.getLogger(DruidConfiguration.class);

    /**
     * 数据库前缀
     */
    private static final String DB_PREFIX = "spring.datasource";

//    @Bean
//    public ServletRegistrationBean druidServlet() {
//        logger.info("init Druid Servlet Configuration ");
//        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/*");
        // IP白名单 (没有配置或者为空，则允许所有访问)
//        servletRegistrationBean.addInitParameter("allow", "");
        // IP黑名单(共同存在时，deny优先于allow)
        //servletRegistrationBean.addInitParameter("deny", "192.168.1.100");
        //控制台管理用户
//        servletRegistrationBean.addInitParameter("loginUsername", "admin");
//        servletRegistrationBean.addInitParameter("loginPassword", "admin");
        //是否能够重置数据 禁用HTML页面上的“Reset All”功能
//        servletRegistrationBean.addInitParameter("resetEnable", "false");
//        return servletRegistrationBean;
//    }

//    @Bean
//    public FilterRegistrationBean filterRegistrationBean() {
//        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
        //添加过滤规则
//        filterRegistrationBean.addUrlPatterns("/*");
        //忽略过滤的格式
//        filterRegistrationBean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
//        return filterRegistrationBean;
//    }

    @Configuration
    class DataSourcesConfig {
        @Bean(name="dataSource")
        @ConfigurationProperties(prefix="spring.datasource")
        public DataSource dataSource(){
            return new DruidDataSource();
        }
        // 配置事物管理器
        @Bean(name="transactionManager")
        public DataSourceTransactionManager transactionManager(){
            return new DataSourceTransactionManager(dataSource());
        }
    }
}
